package com.shizhanzhe.qdd.home.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.qdd.R;
import com.shizhanzhe.qdd.base.UserBean;
import com.shizhanzhe.qdd.home.bean.LoginResponse;
import com.shizhanzhe.qdd.url.Path;
import com.shizhanzhe.qdd.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.qdd.utils.SharedPreferencesUtils;
import com.shizhanzhe.qdd.utils.Util;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by YJ on 2019/1/4.
 */
public class LoginActivity extends Activity {

    @BindView(R.id.iv_close)
    ImageView iv_close;
    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.et_ps)
    EditText et_ps;
    @BindView(R.id.tv_login)
    TextView tv_login;
    @BindView(R.id.ll_qu_reg)
    LinearLayout ll_qu_reg;
    @BindView(R.id.tv_vercode)
    TextView tvVercode;
    @BindView(R.id.ver_login)
    TextView verLogin;
    private CountDownTimer time;
    private int type = 1; //1密码登录  2验证码登录

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .transparentBar()
                .init();
    }

    @OnClick({R.id.iv_close, R.id.tv_login, R.id.ll_qu_reg, R.id.tv_vercode, R.id.ver_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                finish();
                break;
            case R.id.ll_qu_reg:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;
            case R.id.tv_login:
                if (check(et_phone.getText().toString(), et_ps.getText().toString())) {
                    //登录接口
                    login(et_phone.getText().toString(), et_ps.getText().toString());
                }
                break;
            case R.id.tv_vercode:
                if (!"".equals(et_phone.getText().toString())) {
                    if (Util.isMobile(et_phone.getText().toString())) {
                        reCode(et_phone.getText().toString());
                        time = new CountDownTimer(60000, 1000) {
                            @SuppressLint("SetTextI18n")
                            @Override
                            public void onTick(long millisUntilFinished) {
                                tvVercode.setClickable(false);
                                tvVercode.setText(millisUntilFinished / 1000 + "秒");
                            }

                            @Override
                            public void onFinish() {
                                tvVercode.setText("重新验证");
                                tvVercode.setClickable(true);
                            }
                        };
                        time.start();
                    } else {
                        Toast.makeText(this, "手机号输入有误", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.ver_login:
                if (type == 1) {
                    type = 2;
                } else {
                    type = 1;
                }
                loginType();
                break;
                default:
        }
    }

    private boolean check(String phone, String ps) {
        if (Util.isMobile(phone)) {
            if (!"".equals(ps)) {
                return true;
            } else {
                if (type == 1) {
                    Toast.makeText(this, "请输入登录密码", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        } else {
            Toast.makeText(this, "手机号输入有误", Toast.LENGTH_SHORT).show();
            return false;
        }
    }

    private void loginType() {
        if (type == 1) {
            //密码登录
            verLogin.setText("验证码登录");
            tvVercode.setVisibility(View.GONE);
            et_ps.setText("");
            et_ps.setHint("请输入密码");
            //隐藏输入内容
            et_ps.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            //验证码登录
            verLogin.setText("密码登录");
            tvVercode.setVisibility(View.VISIBLE);
            et_ps.setText("");
            et_ps.setHint("请输入验证码");
            //显示输入内容
            et_ps.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }

    //登录接口
    private void login(String phone, String ps) {
        OkHttpDownloadJsonUtil.downloadJson(this, Path.LOGIN(phone,ps,type),
                new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                    @Override
                    public void onsendJson(String json) {
                        Log.d("________",json);
                        if (!"1".equals(json) && !"2".equals(json)) {
                            Gson gson = new Gson();
                            LoginResponse user = gson.fromJson(json, LoginResponse.class);
                            Toast.makeText(LoginActivity.this, user.getInfo(), Toast.LENGTH_SHORT).show();
                            if (user.getStatus() == 1) {
                                UserBean.uid = user.getData().getUid();
                                UserBean.pid = user.getData().getPuid();
                                UserBean.mobile = user.getData().getMobile();
                                UserBean.headimg = user.getData().getAvatar();
                                UserBean.nickname = user.getData().getUname();
                                UserBean.balance = user.getData().getMoney();
                                SharedPreferencesUtils.setParam(LoginActivity.this, "uid", UserBean.uid);
                                SharedPreferencesUtils.setParam(LoginActivity.this, "pid", UserBean.pid);
                                SharedPreferencesUtils.setParam(LoginActivity.this, "mobile", UserBean.mobile);
                                SharedPreferencesUtils.setParam(LoginActivity.this, "headimg", UserBean.headimg);
                                SharedPreferencesUtils.setParam(LoginActivity.this, "nickname", UserBean.nickname);
                                SharedPreferencesUtils.setParam(LoginActivity.this, "balance", UserBean.balance);
                                SharedPreferencesUtils.setDevice(LoginActivity.this);
//                                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                                finish();
                            }
                        } else {
                            Toast.makeText(LoginActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    private void reCode(String phone) {
        String sign=Util.encode(Util.encode(phone+"zxbshare")+"zxbshare");
        OkHttpDownloadJsonUtil.downloadJson(this, Path.SENDPHONECODE(phone,0,sign),
                new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                    @Override
                    public void onsendJson(String json) {
                        Log.d("_____code", json);
                        if (!"1".equals(json) && !"2".equals(json)) {
                            try {
                                JSONObject jsonObject = new JSONObject(json);
                                if (jsonObject.has("info")) {
                                    Toast.makeText(LoginActivity.this, jsonObject.getString("info"), Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                            Toast.makeText(LoginActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

   /* @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.dialog_enter, R.anim.dialog_exit);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        overridePendingTransition(R.anim.dialog_enter, R.anim.dialog_exit);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.dialog_enter, R.anim.dialog_exit);
    }*/
}
